﻿using System.Security.Cryptography;
using System.Text;

namespace tools.algorithm
{
    /// <summary>
    /// RSA 工具类（非对称加密算法）
    /// </summary>
    public static class RsaTool
    {
        /// <summary>
        /// 使用 RSA 算法加密字符串
        /// </summary>
        /// <param name="publicKeyPath">公钥文件完整路径</param>
        /// <param name="plainText">明文</param>
        /// <returns>密文</returns>
        public static string EncryptOfFile(string? publicKeyPath, string? plainText)
        {
            if (!File.Exists(publicKeyPath))
            {
                throw new InvalidOperationException("公钥文件不存在");
            }

            if (string.IsNullOrWhiteSpace(plainText))
            {
                return string.Empty;
            }

            using var rsa = new RSACryptoServiceProvider();
            rsa.ImportFromPem(File.ReadAllText(publicKeyPath));

            byte[] encryptedData = rsa.Encrypt(Encoding.UTF8.GetBytes(plainText), false);
            return Convert.ToBase64String(encryptedData);
        }

        /// <summary>
        /// 使用 RSA 算法解密字符串
        /// </summary>
        /// <param name="privateKeyPath">私钥文件完整路径</param>
        /// <param name="cipherText">密文</param>
        /// <returns>加密前的原文</returns>
        public static string DecryptOfFile(string? privateKeyPath, string? cipherText)
        {
            if (!File.Exists(privateKeyPath))
            {
                throw new InvalidOperationException("私钥文件不存在");
            }

            if (string.IsNullOrWhiteSpace(cipherText))
            {
                return string.Empty;
            }

            var rsa = new RSACryptoServiceProvider();
            rsa.ImportFromPem(File.ReadAllText(privateKeyPath));

            byte[] decryptedData = rsa.Decrypt(Convert.FromBase64String(cipherText), false);
            return Encoding.UTF8.GetString(decryptedData);
        }

        /// <summary>
        /// 使用 RSA 算法加密字符串
        /// </summary>
        /// <param name="publicKey">公钥字符串</param>
        /// <param name="plainText">明文</param>
        /// <returns>密文</returns>
        public static string EncryptOfString(string? publicKey, string? plainText)
        {
            ArgumentException.ThrowIfNullOrWhiteSpace(publicKey);

            if (string.IsNullOrWhiteSpace(plainText))
            {
                return string.Empty;
            }

            using var rsa = new RSACryptoServiceProvider();
            rsa.ImportFromPem(publicKey);

            byte[] encryptedData = rsa.Encrypt(Encoding.UTF8.GetBytes(plainText), false);
            return Convert.ToBase64String(encryptedData);
        }

        /// <summary>
        /// 使用 RSA 算法解密字符串
        /// </summary>
        /// <param name="privateKey">私钥字符串</param>
        /// <param name="cipherText">密文</param>
        /// <returns>加密前的原文</returns>
        public static string DecryptOfString(string? privateKey, string? cipherText)
        {
            ArgumentException.ThrowIfNullOrWhiteSpace(privateKey);

            if (string.IsNullOrWhiteSpace(cipherText))
            {
                return string.Empty;
            }

            var rsa = new RSACryptoServiceProvider();
            rsa.ImportFromPem(privateKey);

            byte[] decryptedData = rsa.Decrypt(Convert.FromBase64String(cipherText), false);
            return Encoding.UTF8.GetString(decryptedData);
        }
    }
}
